/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package callable;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.concurrent.Callable;

/**
 *
 * @author FERRETERIA
 */
public class AIConsulta implements Callable<Integer> {
        private String sql;
        private Connection conexion;
        private String [] datos;
        private int numString;
        private int numFloat;

        public AIConsulta(Connection conexion, String sql, String[] datos, int numString, int numFloat){
            this.sql = sql;
            this.conexion = conexion;
            this.datos = datos;
            this.numString = numString;
            this.numFloat = numFloat;
        }
        public Integer call() throws Exception {
            int id = -1;
            PreparedStatement pstm;
            pstm = conexion.prepareStatement(sql);
            for(int i=0;i<datos.length;i++){
                if (i < numString)//(i < 4)       
                    pstm.setString(i+1, datos[i]);
                else if (i < numFloat) {//(i < 7)
                    if (datos[i] == null)
                        pstm.setNull(i+1, java.sql.Types.FLOAT);
                    else
                        pstm.setFloat(i+1, Float.valueOf(datos[i]));
                }
                else {
                    if (datos[i] == null)
                        pstm.setNull(i+1, java.sql.Types.INTEGER);
                    else
                        pstm.setInt(i+1, Integer.valueOf(datos[i]));
                }
            }
            pstm.execute();
            pstm.clearParameters();
            pstm = conexion.prepareStatement("SELECT @@identity AS id");
            ResultSet res = pstm.executeQuery();
            
            if (res.next()) {
                id = res.getInt("id");
            }
            pstm.close();
            return id;
        }             
}
